<template>
  <div class="app-container">
    <div class="search-con">
      <div class="title">查询条件</div>
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>

        <el-form-item label="账号" prop="code">
          <el-input v-model="queryParams.code" placeholder="请输入账号" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <!-- <el-form-item label="账户显示状态" prop="state">
          <el-select v-model="queryParams.state" placeholder="请选择账户状态" clearable>
            <el-option v-for="dict in account_state" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item> -->
      </el-form>
      <div class="search-btn-con">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="Refresh" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <div class="main-con">
      <div class="title-con">
        <div class="title">基本信息</div>
        <div class="operate-btn-con"></div>
      </div>
      <div class="content-con" v-loading="loading">
        <el-table v-loading="loading" :data="accountsList" @selection-change="handleSelectionChange" height="calc(100% - 0.42rem)">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" label="序号" :index="indexMethod" width="50"></el-table-column>
          <el-table-column label="名称" width="150" align="center" prop="name" />
          <el-table-column label="账号" width="200" align="center" prop="code" />
          <el-table-column label="可用额度" width="120" align="center" prop="availableLimit" />
          <el-table-column label="账单日" width="100" align="center" prop="billDateName" />
          <el-table-column prop="remark" label="近1月POS刷卡记录">
            <template #default="scope">
              <span v-html="formatMultiLineData(scope.row.remark)"></span>
            </template>
          </el-table-column>
          <el-table-column prop="advicePosNames" label="推荐POS刷卡">
            <template #default="scope">
              <span v-html="formatMultiLineData(scope.row.advicePosNames)"></span>
            </template>
          </el-table-column>
          <!-- <el-table-column label="账户显示状态" align="center" prop="state">
            <template #default="scope">
              <dict-tag :options="account_state" :value="scope.row.state" />
            </template>
          </el-table-column> -->
        </el-table>
        <el-pagination
          small
          background
          layout="total,sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup name="Accounts">
import { creditPosList } from '@/api/invest/posmachine'

// eslint-disable-next-line no-unused-vars
import { require } from '@/utils/require'
import dayjs from 'dayjs'
const { proxy } = getCurrentInstance()
const accountsList = ref([])
const open = ref(false)
const loading = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')

const titleDealRecord = ref('')
const openDealRecord = ref(false)

const loadingDealRecord = ref(false)
const tableDealRecordData = ref([])
const dealRecordTotal = ref(0)

const queryDealRecordParams = ref({
  time: '',
  dealType: null,
  dealCategory: null,
  pageNum: 1,
  pageSize: 10
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    status: null,
    type: null,
    state: null
  },
  rules: {
    name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
    type: [{ required: true, message: '账户类型不能为空', trigger: 'change' }],
    status: [{ required: true, message: '账户状态不能为空', trigger: 'blur' }],
    code: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    balance: [{ required: true, message: '余额不能为空', trigger: 'blur' }]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询记账账户列表 */
function getList() {
  loading.value = true
  creditPosList(queryParams.value).then((response) => {
    accountsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    type: null,
    code: null,
    balance: null,
    creditLimit: null,
    availableLimit: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    remark: null,
    accountId: null,
    state: null,
    status: null
  }
  proxy.resetForm('accountsRef')
}

// 分页
const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  getList()
}

//每页显示条数改变
const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  getList()
}
// 序号翻页递增
const indexMethod = (index) => {
  const nowPage = queryParams.value.pageNum //当前第几页，根据组件取值即可
  const nowLimit = queryParams.value.pageSize //当前每页显示几条，根据组件取值即可
  return index + 1 + (nowPage - 1) * nowLimit // 这里可以理解成一个公式
}

function formatMultiLineData(data) {
  if (data != null) {
    return data.replace(/、/g, '<br/>')
  }
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}
getList()
</script>
